package net.display;

import javax.servlet.http.HttpServletRequest;

import net.bo.KPlan;
import net.ko.displays.KObjectDisplay;
import net.ko.http.objects.KRequest;
import net.ko.http.views.KPageList;
import net.ko.kobject.KObject;
import net.ko.utils.KString;

public class DisplayModifPlan extends KObjectDisplay {

	@Override
	public String showInList(KObject ko, String memberName, HttpServletRequest request) {
		String res = super.showInList(ko, memberName, request);
		KPlan plan = (KPlan) ko;

		if ("active".equals(memberName)) {
			if (plan.isActive()) {
				res = "<img src='https://cdn1.iconfinder.com/data/icons/fatcow/16/bullet_valid.png'/>";
			} else {
				res = "<img src='https://cdn1.iconfinder.com/data/icons/lynx/16x16/actions/dialog-close.png'/>";
			}
		} else if ("utilisateur".equals(memberName)) {
			res = plan.getUtilisateur().getNom();
		} else if ("btDelete".equals(memberName)) {
			res = "<img id='btDeletePlan' src='https://cdn1.iconfinder.com/data/icons/momenticons-gloss-basic/momenticons-gloss-basic/16/trashcan-delete.png'/>";
		}

		if ("cocher".equals(memberName)) {
			res = "<input  id='plan-" + plan.getId() + "' type='checkbox' value=" + plan.getId() + " />";

		}

		return res;
	}

	@Override
	public String getCaption(KObject ko, String memberName) {

		String res = super.getCaption(ko, memberName);

		if ("btDelete".equals(memberName)) {
			res = "";
		}

		return res;
	}

	@Override
	public void beforeLoading(Class<? extends KObject> clazz, KPageList list,
			HttpServletRequest request) {

		if (!KString.isNull(KRequest.GETPOST("idPlan", request)))
		{
			String idPlan = KRequest.GETPOST("idPlan", request);
			list.addWhere("id=" + idPlan);
			super.beforeLoading(clazz, list, request);
		} else {
			super.beforeLoading(clazz, list, request);
		}

	}

	@Override
	public String getZone(String zoneName, KPageList list,
			HttpServletRequest request) {
		String result = super.getZone(zoneName, list, request);
		if ("_btReturn".equals(zoneName)) {
			if (!KString.isNull(KRequest.GETPOST("showBtReturn", request))) {
				// String showBtReturn = KRequest.GETPOST("showBtReturn",
				// request);

				result = "<div> <input type='button' class='btn' id='btReturnPlan' value='Retour'/> </div>";

			} else {
				result = "<div> <input style='display:none;' type='button' class='btn' id='btReturnPlan' value='Retour'/> </div>";
			}
		} else if ("_btAdd".equals(zoneName)) {
			if (!KString.isNull(KRequest.GETPOST("showBtReturn", request))) {
				result = "<div> <input style='display:none;' type='button' class='btn' id='btAddPlan' value='Ajouter'/> </div>";
			} else {
				result = "<div> <input type='button' class='btn' id='btAddPlan' value='Ajouter'/> </div>";
			}
		}

		return result;
	}

}
